<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/actions/admin/AdminAction.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/actions/Response.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/model/Service/Service.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/entities/Person.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Error.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Pattern.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/utils/Str.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/model/Service/ServiceFactory.php';

final class PersonRoleSwitchAction extends AdminAction {
	protected function execute(Response $response) {
		$personIdentity = null;
		if(isset($_POST['identity']) && isset($_POST['role'])) {
			$personIdentity = $_POST['identity'];
			$roleIdentity = $_POST['role'];
			$serviceFactory = new ServiceFactory();
			$service = $serviceFactory->getService('PersonService');
			$service->switchRole($personIdentity, $roleIdentity);
		}
		if($personIdentity !== null) {
			$_SESSION['person-identity'] = $personIdentity;
			$response->setActionPath('/person/edit.html');
		} else {
			$response->setActionPath('/person/index.html');
		}
	}
}
?>